package com.wxq.schoolmap;

import java.io.File;
import java.util.List;

import com.wxq.draw.DrawConstants;
import com.wxq.draw.DrawDBTool;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	DrawDBTool dbTool;
	ListView floorLV;
	List<String> nameList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        floorLV = (ListView) findViewById(R.id.floorsLV);
        floorLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
		    	intent.setClass(MainActivity.this, DrawActivity.class);
		    	intent.putExtra("FloorName", nameList.get(position));
		    	startActivity(intent);
			}
		});
          
        
        
        checkDBFile();
        dbTool = new DrawDBTool(this);
        fillFloorsName();
      
        Intent intent = new Intent();
    	intent.setClass(MainActivity.this, DrawActivity.class);
    	intent.putExtra("FloorName", nameList.get(0));
    	startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void checkDBFile() {
    	//设置地图控件
    	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"phone3.db");
    /*	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"0d3e625f-a04c-49ea-9373-ec326e54c7e1.db");
    	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"61cbcb8f-b689-443e-987e-c96f71f4723e.db");
    	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"b0a866dd-d077-45e7-b738-149cf684cb56.db");
    	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"db2d5852-c36d-48c2-a796-b8c48b2421c1.db");
    	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"7e28fd7b-42d4-4d31-b9e7-833fc35650b4.db");
    	DrawDBTool.setupDatabase(this,
    			android.os.Environment.getExternalStorageDirectory()+DrawConstants.DBFolder,
    			"c059c81f-f359-4f0c-8ad0-cb929d19caf3.db");  */
    	
    	//设置SD卡目录
    	
    	
    	String imagesFolder = android.os.Environment.getExternalStorageDirectory()+
    			DrawConstants.ImagesFolder;
    	
    	File imagesDir = new File(imagesFolder);
	
    	if (imagesDir.exists()) {
			Log.d("wxq", imagesDir.toString()+" is exist");
		}
    	else {
    		Log.d("wxq", imagesDir.toString()+" is not exist");
    		
    		if(imagesDir.mkdirs()) {
    			Log.d("wxq", imagesDir.toString()+" creat OK");
    		}
    		else {
    			Log.d("wxq", imagesDir.toString()+" creat failed");
			}
		}
      finish();
	}
    
    public void fillFloorsName() {
		nameList = dbTool.getFloorNames();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_textview1, nameList);
		floorLV.setAdapter(adapter);
		dbTool.close();
	}
}
